package com.freak.wanandroid.model.home;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.freak.httpmanager.BaseViewModel;
import com.freak.httpmanager.callback.ApiCallback;
import com.freak.httpmanager.callback.SubscriberCallBack;
import com.freak.httpmanager.log.LogUtil;
import com.freak.wanandroid.bean.BannerBean;
import com.freak.wanandroid.bean.SystemClassChildrenEntity;
import com.freak.wanandroid.bean.SystemClassifyBean;
import com.freak.wanandroid.model.home.entity.SpecialEntity;
import com.freak.wanandroid.model.home.entity.SpecialSubscriptionListAddedEntity;
import com.freak.wanandroid.model.home.entity.SpecialSubscriptionListHeadEntity;
import com.freak.wanandroid.model.home.entity.SpecialSubscriptionListTitleEntity;

import java.util.ArrayList;
import java.util.List;

public class HomePageViewModel extends BaseViewModel<HomepageRepository> {
    private static final String DEFAULT_SUBSCRIPTION = "进程启动相关";
    protected boolean isGetList = false;
    protected MutableLiveData<List<BannerBean>> mGetBanner = new MutableLiveData<>();
    protected MutableLiveData<String> mGetBannerError = new MutableLiveData<>();

    protected MutableLiveData<List<SystemClassChildrenEntity>> mChildrenMutableLiveData = new MutableLiveData<>();
    protected MutableLiveData<List<MultiItemEntity>> mListMutableLiveData = new MutableLiveData<>();

    public HomePageViewModel(@NonNull Application application) {
        super(application);
    }

    void updateSpecialSubscriptionInfo(SystemClassChildrenEntity classChildrenEntity, int subscription) {
        mRepository.updateSpecialSubscriptionInfo(classChildrenEntity, subscription);
        getSpecialSubscriptionList();
    }

    void getSpecialSubscriptionList() {
        // 查询数据库，如果存在，直接使用数据库数据
        List<SystemClassifyBean> list = mRepository.getSystemClassify();
        if (list != null && list.size() > 0) {
            mListMutableLiveData.postValue(transitionToMultiItemEntity(list));
            return;
        }
        getSystem();
    }

    private List<MultiItemEntity> transitionToMultiItemEntity(List<SystemClassifyBean> list) {
        List<MultiItemEntity> multiItemEntities = new ArrayList<>();
        multiItemEntities.add(new SpecialSubscriptionListTitleEntity("显示在首页"));
        List<SystemClassChildrenEntity> listAddedEntities = mRepository.getSpecialSubscriptionInfo();
        multiItemEntities.add(new SpecialSubscriptionListAddedEntity(listAddedEntities));
        multiItemEntities.add(new SpecialSubscriptionListTitleEntity("可添加的卡片"));
        for (SystemClassifyBean systemClassifyBean : list) {
            List<SystemClassChildrenEntity> systemClassChildrenEntities = mRepository.getSpecialInfo(systemClassifyBean.getParentId());
            if (systemClassChildrenEntities != null && systemClassChildrenEntities.size() > 0) {
                multiItemEntities.add(new SpecialSubscriptionListHeadEntity(systemClassifyBean));
                multiItemEntities.add(new SpecialEntity(systemClassChildrenEntities));
            }
        }
        return multiItemEntities;
    }

    void getSpecialSubscriptionInfo() {
        // 查询数据库，如果存在，直接使用数据库数据
        List<SystemClassifyBean> list = mRepository.getSystemClassify();
        if (list != null && list.size() > 0) {
            List<SystemClassChildrenEntity> subscriptionInfo = mRepository.getSpecialSubscriptionInfo();
            mChildrenMutableLiveData.postValue(subscriptionInfo);
            return;
        }
        getSystem();
    }

    private void updateSubscriptionInfoAfterSave(List<SystemClassifyBean> model) {
        boolean isSuccess = mRepository.saveSpecialSubscription(model, DEFAULT_SUBSCRIPTION);
        if (!isSuccess) {
            LogUtil.i("saveSpecialSubscription error ");
            return;
        }
        if (isGetList) {
            getSpecialSubscriptionList();
            return;
        }
        getSpecialSubscriptionInfo();
    }

    private void getSystem() {
        mRepository.getSystem(new SubscriberCallBack<>(new ApiCallback<List<SystemClassifyBean>>() {
            @Override
            public void onSuccess(List<SystemClassifyBean> model) {
                updateSubscriptionInfoAfterSave(model);
            }

            @Override
            public void onFailure(String msg) {
                LogUtil.i("HomePageViewModel getSystem error " + msg);
            }
        }));
    }

    public void getBanner() {
        mRepository.getBanner(new SubscriberCallBack<>(new ApiCallback<List<BannerBean>>() {
            @Override
            public void onSuccess(List<BannerBean> model) {
                mGetBanner.postValue(model);
            }

            @Override
            public void onFailure(String msg) {
                mGetBannerError.postValue(msg);
            }
        }));
    }
}
